package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.logistics.entity.LogisticsOrderLog;
import com.ssy.lingxi.logistics.model.enums.LogisticsOrderStatusEnum;
import com.ssy.lingxi.logistics.repository.LogisticsOrderLogRepository;
import com.ssy.lingxi.logistics.service.ILogisticsOrderLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 物流单流转记录服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/26
 */
@Slf4j
@Service
public class LogisticsOrderLogServiceImpl implements ILogisticsOrderLogService {

    @Resource
    private LogisticsOrderLogRepository logisticsOrderLogRepository;

    @Override
    public void createSubmitLogisticsOrderLog(Long logisticsOrderId, Integer status, String operation, UserLoginCacheDTO loginUser) {
        // 历史流转记录
        LogisticsOrderLog logisticsOrderLog = buildLogisticsOrderLog(loginUser, logisticsOrderId, status, operation, "");
        logisticsOrderLogRepository.saveAndFlush(logisticsOrderLog);
    }

    @Override
    public void createConfirmLogisticsOrderLog(Long logisticsOrderId, Integer status, String operation, String remark, UserLoginCacheDTO loginUser) {
        // 历史流转记录
        LogisticsOrderLog logisticsOrderLog = buildLogisticsOrderLog(loginUser, logisticsOrderId, status, operation, remark);
        logisticsOrderLogRepository.saveAndFlush(logisticsOrderLog);
    }

    private LogisticsOrderLog buildLogisticsOrderLog(UserLoginCacheDTO loginUser, Long logisticsOrderId, Integer status, String operation, String remark) {
        LogisticsOrderLog logisticsOrderLog = new LogisticsOrderLog();
        logisticsOrderLog.setOrderId(logisticsOrderId);
        logisticsOrderLog.setMemberId(loginUser.getMemberId());
        logisticsOrderLog.setRoleId(loginUser.getMemberRoleId());
        logisticsOrderLog.setOperatorRoleName(loginUser.getMemberRoleName());
        logisticsOrderLog.setStatus(status);
        logisticsOrderLog.setStatusName(LogisticsOrderStatusEnum.getCodeMsg(status));
        logisticsOrderLog.setOperation(operation);
        logisticsOrderLog.setOperateTime(System.currentTimeMillis());
        logisticsOrderLog.setRemark(remark);
        return logisticsOrderLog;
    }
}